package com.bw.mallshop.mvvm.viewmodel;

import android.os.Bundle;
import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.entity.ForgetPWDEntity;
import com.bw.mallshop.mvvm.model.ForgetPWDModel;
import com.bw.mallshop.mvvm.view.ResetActivity;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ForgetPWDViewModel extends BaseViewModel<ForgetPWDModel> {

    public ObservableField<String>phone = new ObservableField();
    public ObservableField<String>timeStr = new ObservableField<>();
    public ObservableField<String>verify = new ObservableField<>();

    public void next(){
        if(TextUtils.isEmpty(phone.get())){
            showErro("手机号为空");
            return;
        }
        //获取验证码
        Map<String,Object>map = new HashMap<>();
        map.put("mobile",phone.get());
        map.put("verifyCode","123456");
        m.requestForget(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void finish(){
        finishPage();
    }

    public void verify(){
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof ForgetPWDEntity){
            ForgetPWDEntity pwdEntity = (ForgetPWDEntity) entity;
            if (pwdEntity.message.equals("验证成功")){
                Bundle bundle = new Bundle();
                bundle.putString("phone",phone.get());
                startPage(ResetActivity.class,bundle);
                finishPage();
            }else {
                showErro(pwdEntity.message);
            }
        }
    }
}
